﻿using SCM.Common;
using SCM.Common.Extensions;
using SCM.Model;
using SCM.Model.DBEntity;
using SCM.Servie.IBusiness;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;

namespace SCM.Servie.Business
{
    /// <summary>
    /// 材质服务
    /// </summary>
    public class MaterialHomogeneousService : IMaterialHomogeneousService
    {
        private readonly DbContext dal = new DbContext();

        public string Add(T_Material_Homogeneous model)
        {
            return dal.Insertable(model).ExecuteReturnEntity().ID;
        }

        public bool Delete(List<string> ids)
        {
            try
            {
                dal.BeginTran();

                bool result = dal.Updateable<T_Material_Homogeneous>(new
                {
                    DataStatus = DataStatusEnum.Deleted,
                    ModifyTime = DateTime.Now,
                }).Where(p => ids.Contains(p.ID)).ExecuteCommand() > 0;

                if (result == false)
                {
                    dal.RollbackTran();
                    return false;
                }

                result = dal.Updateable<T_Material_HomogeneousDtl>(new
                {
                    DataStatus = DataStatusEnum.Deleted,
                }).Where(p => ids.Contains(p.ID)).ExecuteCommand() > 0;  

                if (result == false)
                {
                    dal.RollbackTran();
                    return false;
                }

                dal.CommitTran();
                return true;
            }
            catch (Exception ex)
            {
                dal.RollbackTran();
                throw ex;
            }
        }

        public T_Material_Homogeneous GetById(string id)
        {
            return dal.Get<T_Material_Homogeneous>(id);
        }

        public List<T_Material_Homogeneous> GetList(string companyID)
        {
            Expression<Func<T_Material_Homogeneous, bool>> whereExp = (a) => a.DataStatus == DataStatusEnum.Valid;
            if (!string.IsNullOrWhiteSpace(companyID))
            {
                whereExp = whereExp.Compose((a) => a.CompanyID == companyID, Expression.AndAlso);
            }
            return dal.Queryable<T_Material_Homogeneous>().Where(whereExp).ToList();
        }

        public PageData<T_Material_Homogeneous> Query(string companyID, string name, string materialCatalogID, PageInfo page)
        {
            int totalCount = 0;
            Expression<Func<T_Material_Homogeneous, T_Material_Catalog, bool>> whereExp = (a, b) => a.DataStatus == DataStatusEnum.Valid;
            if (!string.IsNullOrWhiteSpace(companyID))
            {
                whereExp = whereExp.Compose((a, b) => a.CompanyID == companyID, Expression.AndAlso);
            }

            if (!string.IsNullOrWhiteSpace(materialCatalogID))
            {
                whereExp = whereExp.Compose((a, b) => a.MaterialCatalogID == materialCatalogID, Expression.AndAlso);
            }

            if (!string.IsNullOrWhiteSpace(name))
            {
                whereExp = whereExp.Compose((a, b) => a.Name.StartsWith(name), Expression.AndAlso);
            }

            var list = dal.Queryable<T_Material_Homogeneous, T_Material_Catalog>((a, b) => new object[]
            {
                JoinType.Left,a.MaterialCatalogID == b.Identifier,
            })
            .Where(whereExp)
            .OrderBy(a => a.CreateTime, OrderByType.Desc)
            .Select((a, b) => new T_Material_Homogeneous()
            {
                ID = a.ID,
                CompanyID = a.CompanyID,
                Name = a.Name,
                MaterialCatalogID = b.Identifier,
                MaterialCatalogName = b.CName,
                CreateTime = a.CreateTime,
                DataStatus = a.DataStatus
            })
            .ToPageList(page.PageIndex, page.PageSize, ref totalCount);
            page.DataCount = totalCount;
            return new PageData<T_Material_Homogeneous>() { Data = list, Page = page };
        }

        public bool Update(T_Material_Homogeneous model)
        {
            return dal.Updateable<T_Material_Homogeneous>(p => new T_Material_Homogeneous()
            {
                Name = model.Name,
                MaterialCatalogID = model.MaterialCatalogID,
                ModifyUserId = model.ModifyUserId,
                ModifyUserName = model.ModifyUserName,
            }).Where(p => p.ID == model.ID).ExecuteCommand() > 0;
        }
    }
}
